package cn.lili.modules.goods.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.modules.goods.entity.dos.Category;
import cn.lili.modules.goods.entity.dos.DraftGoods;
import cn.lili.modules.goods.entity.dos.GoodsGallery;
import cn.lili.modules.goods.entity.dos.Wholesale;
import cn.lili.modules.goods.entity.dto.DraftGoodsSearchParams;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.vos.DraftGoodsVO;
import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
import cn.lili.modules.goods.mapper.DraftGoodsMapper;
import cn.lili.modules.goods.service.*;
import cn.lili.mybatis.util.PageUtil;
import cn.lili.mybatis.util.SceneHelp;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 草稿商品业务层实现
 *
 * @author paulG
 * @since 2020/12/19
 **/
@Service
@RequiredArgsConstructor
public class DraftGoodsServiceImpl extends ServiceImpl<DraftGoodsMapper, DraftGoods> implements DraftGoodsService {
    /**
     * 分类
     */
    private final CategoryService categoryService;
    /**
     * 商品相册
     */
    private final GoodsGalleryService goodsGalleryService;
    /**
     * 规格商品
     */
    private final GoodsSkuService goodsSkuService;

    /**
     * 批发规则
     */
    private final WholesaleService wholesaleService;


    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public void saveGoodsDraft(DraftGoods draftGoods) {
        //对象鉴权
        if (draftGoods.getId() != null) {
            DraftGoods old = this.getDraftGoods(draftGoods.getId());
            SceneHelp.objectAuthentication(old);
        }
        //数据处理
        SceneHelp.objectHandler(draftGoods);
        this.saveOrUpdate(draftGoods);
    }

    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public void deleteGoodsDraft(String id) {
        this.removeById(id);
    }

    @Override
    public DraftGoodsVO getDraftGoods(String id) {

        DraftGoods draftGoods = this.getById(id);
        DraftGoodsVO draftGoodsVO = new DraftGoodsVO();
        BeanUtil.copyProperties(draftGoods, draftGoodsVO);
        //商品id
        draftGoodsVO.setId(draftGoods.getId());
        //商品相册赋值
        List<String> images = new ArrayList<>();
        List<GoodsGallery> galleryList = goodsGalleryService.goodsGalleryList(id);
        for (GoodsGallery goodsGallery : galleryList) {
            images.add(goodsGallery.getOriginal());
        }
        draftGoodsVO.setGoodsGalleryList(images);
        //商品sku赋值
        List<GoodsSkuVO> goodsListByGoodsId = goodsSkuService.getGoodsListByGoodsId(GoodsSearchParams.builder().goodsId(id).build());
        if (goodsListByGoodsId != null && !goodsListByGoodsId.isEmpty()) {
            draftGoodsVO.setSkuList(goodsListByGoodsId);
        }
        //商品分类名称赋值
        List<String> categoryName = new ArrayList<>();
        String categoryPath = draftGoods.getCategoryPath();
        if (CharSequenceUtil.isNotEmpty(categoryPath)) {
            String[] strArray = categoryPath.split(",");
            List<Category> categories = categoryService.listByIds(Arrays.asList(strArray));
            for (Category category : categories) {
                categoryName.add(category.getName());
            }
            draftGoodsVO.setCategoryName(categoryName);
        }

        //参数非空则填写参数
        if (CharSequenceUtil.isNotEmpty(draftGoods.getParams())) {
            draftGoodsVO.setGoodsParamsDTOList(JSONUtil.toList(draftGoods.getParams(), GoodsParamsDTO.class));
        }

        //供应商商品，且支持采购，则描述采购规则
        if (draftGoods.getIsProxyGoods() && draftGoods.getSupportPurchase()) {
            List<Wholesale> wholesaleList = wholesaleService.findByGoodsId(id);
            if (CollUtil.isNotEmpty(wholesaleList)) {
                draftGoodsVO.setWholesaleList(wholesaleList);
            }
        }
        return draftGoodsVO;
    }

    @Override
    public Page<DraftGoods> getDraftGoods(DraftGoodsSearchParams searchParams) {
        return this.page(PageUtil.initPage(searchParams), searchParams.queryWrapper());
    }


}
